<?php
/*
*generate pinCode for tongyi CORP 
*by River 
*2014-1-20
*
*/
class PloyPinGenerate extends AppModel {
	public $name = 'PloyPinGenerate';
	public $useTable = 'ploy_pin';
	public $primaryKey = 'pin_id';

	private static $tmp_file_path = '../tmp/tmpPinCode.txt';
/**
 * [generate_secret description]
 * simple generate 	key	
 * @return [str] [key]
 */	
	public function generate_secret()
	{
		$n = mt_rand(4,7);
        $str = '';
        for ($j=0; $j<$n; ++$j)
        {
            $str .= chr(rand(21,126));
        }
        return $str;
	}
	/*
	*生成pinCode
	*parama：
	*prefix
	*当前code在此批code中的队列号
	*key
	*return:
	*string   pinCode
	*/
	public function _producePinCode($prefix,$id,$key)
	{
		$Id28=str_pad($this->dec2b28($id),6,'0', STR_PAD_LEFT);
		$checkCode=md5($key.$Id28.$prefix);
		$checkCode=strtoupper(substr($checkCode,0,4));
		$pinCode=$prefix.$checkCode.$Id28;
		return $pinCode;
	}
	/*
	版本一
	*发送TEXT
	*parama：
	*array: PINCODE
	*生成的文件名
	*return:
	*/
	public function outPutTxt($aPinCodes,$fileName)
	{
		$content='';
		$arrCount=count($aPinCodes);
		for ($i=0; $i <$arrCount; $i++) { 
			$content.=$aPinCodes[$i]."\r\n"; //\r\n 为换行
		}
		$fileName.='_'.$arrCount.'.txt';
		$this->sendTxt($fileName);
		echo $content;
		unset($content);
	}
/*
版本二
*开始之前检查这个文件是否存在，存在则删除
*/
	public function startGenerate() {
		if (file_exists(self::$tmp_file_path)) {
			unlink(self::$tmp_file_path);
		}
	}
/*
版本二
*写入text
*/	
	public function outPutText($codes = array()) {
		$file_handler = fopen(self::$tmp_file_path, 'a+');
		$string = implode("\r\n", $codes);
		$string .= "\r\n";
		fwrite($file_handler, $string);
		fclose($file_handler);
	}
/*
* 版本二
*下载text
*/
	public function downloadFile($filename) {
		$this->sendTxt($filename);
		$file_handler = fopen(self::$tmp_file_path, 'r');
		$block = 4096;
		$block_content = '';
		while(!feof($file_handler)) {
			$block_content = fread($file_handler, $block);
			echo $block_content;
		}
	}

	public function updateQuantity($pin_id,$newQuantity)
	{
		$sql='update `ploy_pin` set pin_gen_quantity = '.$newQuantity.' where pin_id= '.$pin_id;
		$this->query($sql);
	}
	 /**
     * 发送下载文本文件
     * @param $filename 文件名
     */
    private function sendTxt($filename)
    {
        header("Content-type: text");
        header("Content-Disposition: attachment; filename=$filename");
        header("Expires: 0");
        header("Cache-Control: must-revalidate, post-check=0,pre-check=0");
        header("Pragma: public");
    }
	/*
	*批次列表
	*/
	public function batchList()
	{
		return array(
			'null'=>'请选择批次',
			'01'=>'01',
			'02'=>'02',
			'03'=>'03',
			'04'=>'04',
			'05'=>'05',
			'06'=>'06',
			'07'=>'07',
			'08'=>'08',
			'09'=>'09',
			'10'=>'10',
			'11'=>'11',
			'12'=>'12',
		);
	}
	/*
	*返回字符
	*/
	public function word28()
	{
		return array(0=>"4",1=>"5",2=>"6",3=>"7",4=>"9",5=>"A",6=>"B",7=>"C",8=>"D",9=>"E",10=>"F",11=>"G",12=>"H",13=>"J",14=>"K",15=>"L",16=>"M",17=>"N",18=>"P",19=>"Q",20=>"R",21=>"S",22=>"T",23=>"U",24=>"V",25=>"W",26=>"X",27=>"Y");
	}
	/*
	*将10进制转换为34进制
	*2013-1-21
	*/
	public function dec2b28($id)
	{
		$b28=$this->word28();
		$retstr='';
		if ($id>0) {
			while($id>0)
            {
                $retstr = $b28[($id % 28)].$retstr;
                $id = floor($id/28);
            }
		}
		else
        {
            $retstr = "0";
        }
        return $retstr;
	}
}
?>